package com.quantstack.llcommonlibrary.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.text.Editable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.quantstack.llcommonlibrary.R;
import com.quantstack.llcommonlibrary.event.OnClickEvent;

/**
 * @author wu_ming_zhi_bei
 * @date 2021/2/27 11:33
 * @Notes 联系人页面
 */
public class ContactItem extends LinearLayout {
    private Context context;
    private TextView tvTitle;
    private TextView tvNameTitle;
    private TextView tvNameValue;
    private TextView tvNumberTitle;
    private TextView tvNumberValue;
    private TextView tvRelationshipTitle;
    private TextView tvRelationshipValue;

    private TextView tvNameError;
    private TextView tvNumberError;
    private TextView tvRelationshipError;
    private LinearLayout llTop;
    private LinearLayout llName;
    public ContactItem(Context context) {
        this(context,null);
    }

    public ContactItem(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);
    }

    public ContactItem(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context,attrs);
    }

    private void init(Context context, AttributeSet attrs) {
        this.context = context;
        LayoutInflater.from(context).inflate(R.layout.layout_contact_item,this);
        tvTitle = (TextView)findViewById(R.id.tv_title);
        tvNameTitle = (TextView)findViewById(R.id.tv_name_title);
        tvNameValue = (TextView)findViewById(R.id.tv_name_value);
        tvNumberTitle = (TextView)findViewById(R.id.tv_number_title);
        tvNumberValue = (TextView)findViewById(R.id.tv_number_value);
        tvRelationshipTitle = (TextView)findViewById(R.id.tv_relationship_title);
        tvRelationshipValue = (TextView)findViewById(R.id.tv_relationship_value);
        llTop = (LinearLayout)findViewById(R.id.ll_top);
        llName = (LinearLayout)findViewById(R.id.ll_name);

        tvNameError = (TextView)findViewById(R.id.tv_name_error);
        tvNumberError = (TextView)findViewById(R.id.tv_number_error);
        tvRelationshipError = (TextView)findViewById(R.id.tv_relationship_error);

        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.ContactItem);
        String title = a.getString(R.styleable.ContactItem_title);
        tvTitle.setText(title);

        String nameTitle = a.getString(R.styleable.ContactItem_nameTitle);
        tvNameTitle.setText(nameTitle);

        String numberTitle = a.getString(R.styleable.ContactItem_numberTitle);
        tvNumberTitle.setText(numberTitle);

        String relationshipTitle = a.getString(R.styleable.ContactItem_relationshipTitle);
        tvRelationshipTitle.setText(relationshipTitle);
        a.recycle();
        /**
         * 隐藏错误
         */
        tvNameValue.addTextChangedListener(new SimpleTextWatcher() {
            @Override
            public void afterTextChanged(Editable s) {
                super.afterTextChanged(s);
                setNameError("");
            }
        });
        tvNumberValue.addTextChangedListener(new SimpleTextWatcher() {
            @Override
            public void afterTextChanged(Editable s) {
                super.afterTextChanged(s);
                setNameError("");
            }
        });
        /**
         * 绑定事件
         */
        llTop.setOnClickListener(new OnClickEvent() {
            @Override
            public void singleClick(View v) {
                if(onContactClickListener!=null){
                    onContactClickListener.onTopClick();
                }
            }
        });
        tvNameValue.setOnClickListener(new OnClickEvent() {
            @Override
            public void singleClick(View v) {
                if(onContactClickListener!=null){
                    onContactClickListener.onNameClick();
                }
            }
        });
        tvNumberValue.setOnClickListener(new OnClickEvent() {
            @Override
            public void singleClick(View v) {
                if(onContactClickListener!=null){
                    onContactClickListener.onNumberClick();
                }
            }
        });
        tvRelationshipValue.setOnClickListener(new OnClickEvent() {
            @Override
            public void singleClick(View v) {
                if(onContactClickListener!=null){
                    onContactClickListener.onRelationshipClick();
                }
            }
        });
        llName.setOnClickListener(new OnClickEvent() {
            @Override
            public void singleClick(View v) {
                if(onContactClickListener!=null){
                    onContactClickListener.onEditNameClick();
                }
            }
        });
    }

    /**
     * 设置姓名错误信息
     * @param msg
     */
    public void setNameError(String msg){
        if(TextUtils.isEmpty(msg)){
            tvNameError.setVisibility(GONE);
        }else{
            tvNameError.setVisibility(VISIBLE);
            tvNameError.setText(msg);
        }

    }

    /**
     * 设置电话号码错误信息
     * @param msg
     */
    public void setNumberError(String msg){
        if(TextUtils.isEmpty(msg)){
            tvNumberError.setVisibility(GONE);
        }else{
            tvNumberError.setVisibility(VISIBLE);
            tvNumberError.setText(msg);
        }

    }

    /**
     * 设置关系错误信息
     * @param msg
     */
    public void setRelationshipError(String msg){
        if(TextUtils.isEmpty(msg)){
            tvRelationshipError.setVisibility(GONE);
        }else{
            tvRelationshipError.setVisibility(VISIBLE);
            tvRelationshipError.setText(msg);
        }
    }

    /**
     * 获取name
     */
    public String getName(){
        return tvNameValue.getText().toString().trim();
    }
    /**
     * 设置name
     */
    public void setName(String msg){
        tvNameValue.setText(msg);
    }

    /**
     * 获取电话号码
     */
    public String getNumber(){
        return tvNumberValue.getText().toString().trim();
    }

    /**
     * 设置电话号码
     */
    public void setNumber(String msg){
        tvNumberValue.setText(msg);
    }
    /**
     * 获取关系
     */
    public String getRelationship(){
        return tvRelationshipValue.getText().toString().trim();
    }
    /**
     * 设置关系
     */
    public void setRelationship(String msg){
        tvRelationshipValue.setText(msg);
    }

    private OnContactClickListener onContactClickListener;
    public void setOnContactClickListener(OnContactClickListener onContactClickListener) {
        this.onContactClickListener = onContactClickListener;
    }
    public interface OnContactClickListener{
        void onNameClick();
        void onNumberClick();
        void onRelationshipClick();
        void onTopClick();
        void onEditNameClick();
    }
}
